<template>
  <div class="mod-config">
      <el-tabs v-model="activeName" @tab-click="handleClick">
        <el-tab-pane label="商品库存" name="first">
          <StockTable ref="stockTable"></StockTable>
        </el-tab-pane>
        <el-tab-pane label="库存记录" name="second">
          <StockRecord ref="stockRecord"></StockRecord>
        </el-tab-pane>
        <el-tab-pane label="出入、移库管理" name="third">
          <StockApplyForm ref="stockApplyForm"></StockApplyForm>
        </el-tab-pane>
      </el-tabs>
  </div>
</template>

<script>
  import StockTable from './stock/stock-table'
  import StockRecord from './stock/stockRecord'
  import StockApplyForm from './stock/stockApplyForm'
  export default {
    components: {
      StockTable,StockRecord,StockApplyForm
    },
    data() {
      return {
        dataForm: {
          shopId: null,
          goodsName: ''
        },
        activeName: 'first',
        visible: false,
        disabled: false,
        dataList: [],
        // tableData: [],
        dataListLoading: false,
      }
    },
    methods: {
      // 标签点击
      handleClick(tab, event){
        let tabname = tab.name;
        if(tabname == 'first'){
          this.$refs.stockTable.init();
        } else if(tabname == 'second'){
          this.$refs.stockRecord.init();
        } else if(tabname == 'third'){
          this.$refs.stockApplyForm.init();
        }
      },
      // 获取数据列表
      getDataList() {
        this.dataList = []
        if(this.dataForm.shopId == null){
          return
        }
        this.dataListLoading = true;
        this.$http({
          url: this.$http.adornUrl(`/merchant/depot/stock/${this.dataForm.shopId}`),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            if(data.data && data.data.length > 0){
              this.dataList = data.data;
            }
          }
          this.dataListLoading = false;
        })
      },
      // 初始化库存
      initStock() {
        if(this.dataForm.shopId == null){
          return
        }
        this.disabled = true
        this.$http({
          url: this.$http.adornUrl(`/merchant/depot/init/${this.dataForm.shopId}`),
          method: 'get',
          params: this.$http.adornParams({})
        }).then(({
          data
        }) => {
          if (data && data.code === 0) {
            this.$message.success('后台正在异步初始化库存，清稍后');
            setTimeout(() => {
                this.getStatus()
              }, 1500)
          } else {
            this.$message.error(data.msg);
            this.disabled = false
          }
          
        }).catch(() => {
          this.$message.error('连接超时，请稍后再试');
          this.disabled = false
        });
      },
    }
  }
</script>
